package com.codeyang.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codeyang.domain.User;
import com.codeyang.dto.UserDto;
import com.codeyang.mapper.UserMapper;
import com.codeyang.service.UserService;
import com.codeyang.vo.DataGridView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 描述: TO-删除标志-DO
 *
 * @author CodeYang_Site
 * @version 2021/4/20 15:34
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 使用用户的名称查询用户据
     * 这里是通过手机号码来登录的
     *
     * @param userLoginName 广义的用户名
     * @return user对象 null
     */
    @Override
    public User findUserByUserName(String userLoginName) {

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(User.COL_PHONE, userLoginName);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public DataGridView lisPage(UserDto userDto) {
        Page<User> page = new Page<>(userDto.getPageNum(), userDto.getPageSize());
        // 排序 更新时间
        page.addOrder(OrderItem.desc(User.COL_UPDATE_TIME));
        QueryWrapper<User> qw = new QueryWrapper<>();
        // 部门id
        qw.eq(userDto.getDeptId() != null, User.COL_DEPT_ID, userDto.getDeptId());
        qw.like(StringUtils.isNotBlank(userDto.getUserName()), User.COL_USER_NAME, userDto.getUserName());
        qw.like(StringUtils.isNotBlank(userDto.getPhone()), User.COL_PHONE, userDto.getPhone());
        qw.eq(StringUtils.isNotBlank(userDto.getStatus()), User.COL_STATUS, userDto.getStatus());
        qw.between(
                userDto.getBeginTime() != null && userDto.getEndTime() != null,
                User.COL_CREATE_TIME,
                userDto.getBeginTime(),
                userDto.getEndTime()
        );

        userMapper.selectPage(page, qw);
        return new DataGridView(page.getTotal(), page.getRecords());
    }

    @Override
    public boolean saveUser(UserDto userDto) {
        User user = new User();
        BeanUtils.copyProperties(userDto, user);
        // 创建人+时间
        user.setCreateTime(new Date());
        user.setCreateBy(userDto.getSimpleUser().getUserName());
        // 修改时间
        user.setUpdateTime(user.getCreateTime());
        return userMapper.insert(user) > 0;
    }

    @Override
    public boolean updateUser(UserDto userDto) {
        User user = new User();
        BeanUtils.copyProperties(userDto, user);

        // 修改时间
        user.setUpdateTime(new Date());
        user.setUpdateBy(userDto.getSimpleUser().getUserName());
        return userMapper.updateById(user) > 0;
    }
}
